package com.sanjiahantang.youjiaclient.module.common.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.views.SettingsView;
import com.sanjiahantang.youjiaclient.module.login.views.impl.ChangePasswordActivity;
import com.sanjiahantang.youjiaclient.module.common.views.impl.SettingsActivity;
import com.sanjiahantang.youjiaclient.module.me.account.views.impl.AccountActivity;
import com.sanjiahantang.youjiaclient.module.me.address.views.impl.AddressListActivity;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zy on 2017/9/4.
 */

public class SettingsPresenter extends BaseMvpPresenter<SettingsView> {
    public void goMyInfo(BaseMvpActivity context) {
        goPageSimply(context, AccountActivity.class);
    }

    public void goChangePSD(BaseMvpActivity context) {
        ChangePasswordActivity.changeLoginPsd(context);
    }

    public void goChangePayPSD(Context context){
        ChangePasswordActivity.changePayPsd(context);
    }

    public void goAddressManage(BaseMvpActivity context) {
        goPageSimply(context, AddressListActivity.class);
    }

    public void logout(final Activity activity) {
        if (!TextUtils.isEmpty(ConfigValue.DATA_KEY)) {
            Utils.showDialog(activity, "提示", "确定退出登录吗？", "", "", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Utils.dismissDialog();
                    switch (view.getId()) {
                        case R.id.txtDialogCancel:
                            break;
                        case R.id.txtDialogSure:
                            //解绑tag
                            Set<String> set = new HashSet<>();
                            set.add("0");
                            JPushInterface.setTags(activity,0,set);
                            JPushInterface.setAlias(activity,0,"");
                            ConfigValue.userInfo = null;
                            ConfigValue.DATA_KEY = "";
                            SPUtils.remove(activity, SPConfig.PHONE);
                            SPUtils.remove(activity, SPConfig.USERNAME);
                            SPUtils.remove(activity, SPConfig.KEY);
                            Utils.dismissDialog();

                            Intent mIntent = new Intent(ConfigValue.ACTION_ALTER_CARTGOODS_NUMS);
                            mIntent.putExtra("cartgoodsnum", "0");
                            //发送广播
                            activity.sendBroadcast(mIntent);
                            activity.finish();


                            break;
                    }
                }
            });
        }
    }
}
